#include "fsxwsmainwindow.h"
#include "ui_fsxwsmainwindow.h"
using namespace FsxWsGUI;

//
// Add single aircraft
//
void FsxWsMainWindow::addAircraftToView(const Aircraft& aircraft)
{
    QList<QStandardItem*> *row = new QList<QStandardItem*>();
    FSXAircraftStringAndMiscData sd = aircraft.getStringMiscData();
    FSXAircraftPositionAndSpeedData ps = aircraft.getPositionSpeedData();
    FSXAircraftInstrumentationData in = aircraft.getInstrumentationData();
    FSXAircraftAvionicsData av = aircraft.getAvionicsData();
    int idx =0;
    row->insert(idx++, new QStandardItem(sd.atcId)); // callsign
    row->insert(idx++, new QStandardItem(sd.atcModel));
    row->insert(idx++, new QStandardItem(sd.atcType));
    row->insert(idx++, new QStandardItem(QString::number(aircraft.getId())));
    row->insert(idx++, new QStandardItem(aircraft.getLastUpdatedFormattedTimeUtc()));
    row->insert(idx++, new QStandardItem(aircraft.getLastUpdatedFormattedTimeLocal()));
    row->insert(idx++, new QStandardItem(QString::number(aircraft.waypointsCount())));
    row->insert(idx++, new QStandardItem(av.transponderCodeFormatted()));
    row->insert(idx++, new QStandardItem(QString::number(ps.planeLatitudeDeg())));
    row->insert(idx++, new QStandardItem(QString::number(ps.planeLongitudeDeg())));
    row->insert(idx++, new QStandardItem(QString::number(ps.planeAltitudeFt,'f',0)));
    row->insert(idx++, new QStandardItem(QString::number(ps.planeAltitudeAboveGroundFt,'f',0)));
    row->insert(idx++, new QStandardItem(QString::number(in.indicatedAltitudeFt,'f',0)));
    row->insert(idx++, new QStandardItem(QString::number(ps.groundAltitudeM,'f',1)));
    row->insert(idx++, new QStandardItem(QString::number(in.kohlsmanSettingMb,'f',2)));
    row->insert(idx++, new QStandardItem(QString::number(ps.groundVelocityKts,'f', 1))); // GS
    row->insert(idx++, new QStandardItem(QString::number(in.airspeedTrueKts,'f', 1))); // TAS
    row->insert(idx++, new QStandardItem(QString::number(in.airspeedIndicatedKts,'f', 1))); // IAS
    row->insert(idx++, new QStandardItem(QString::number(ps.planeHeadingTrueDeg(),'f',1)));
    row->insert(idx++, new QStandardItem(QString::number(ps.planePitchDeg(),'f',1)));
    row->insert(idx++, new QStandardItem(QString::number(ps.planeBankDeg(),'f',1)));
    row->insert(idx++, new QStandardItem(QString::number(ps.angleOfAttackDeg(),'f',1)));
    row->insert(idx++, new QStandardItem(QString::number(ps.sideslipAngleDeg(),'f',1)));
    row->insert(idx++, new QStandardItem(ps.simOnGround ? "X" : " "));
    row->insert(idx++, new QStandardItem(ps.surfaceText()));
    row->insert(idx++, new QStandardItem(av.com1ActiveMHzFormatted()));
    row->insert(idx++, new QStandardItem(av.com2ActiveMHzFormatted()));
    row->insert(idx++, new QStandardItem(av.nav1ActiveMHzFormatted()));
    row->insert(idx++, new QStandardItem(av.nav2ActiveMHzFormatted()));
    row->insert(idx++, new QStandardItem(av.adf1ActiveKHzFormatted()));
    row->insert(idx++, new QStandardItem(av.adf2ActiveKHzFormatted()));

    FsxWsMainWindow::_aircraftsViewDataModel->appendRow(*row);
}

//
// Add single aircraft
//
void FsxWsMainWindow::addWaypointToView(QStandardItemModel* waypointModel, const Waypoint& waypoint)
{
    QList<QStandardItem*> *row = new QList<QStandardItem*>();
    int idx =0;
    row->insert(idx++, new QStandardItem(waypoint.getFormattedTimeUtc()));
    row->insert(idx++, new QStandardItem(waypoint.getFormattedTimeLocal()));
    row->insert(idx++, new QStandardItem(QString::number(waypoint.getLatitudeDeg())));
    row->insert(idx++, new QStandardItem(QString::number(waypoint.getLongitudeDeg())));
    row->insert(idx++, new QStandardItem(QString::number(waypoint.getAltitudeFt(),'f',0)));
    row->insert(idx++, new QStandardItem(waypoint.onGnd() ? "X" : " "));
    row->insert(idx++, new QStandardItem(QString::number(waypoint.getGroundspeedKts(),'f', 2))); // GS
    row->insert(idx++, new QStandardItem(QString::number(waypoint.getAirspeedTrueKts(),'f',2)));
    row->insert(idx++, new QStandardItem(QString::number(waypoint.getAirspeedIndicatedKts(),'f',2)));
    row->insert(idx++, new QStandardItem(QString::number(waypoint.getPitchDeg(),'f', 2)));
    row->insert(idx++, new QStandardItem(QString::number(waypoint.getBankDeg(),'f', 2)));
    row->insert(idx++, new QStandardItem(waypoint.hasValidPositionAndSpeedData() ? "X" : ""));
    row->insert(idx++, new QStandardItem(waypoint.hasValidInstrumentationData() ? "X" : ""));
    waypointModel->appendRow(*row);
}

//
// Add aircrafts
//
void FsxWsMainWindow::addAircraftsToView(const QList<Aircraft>& aircrafts)
{
    // update
    Aircraft a;
    foreach( a, aircrafts ){
        // check whether it shall be displayed
        if (FsxWsMainWindow::_fsxWsSettings &&
                FsxWsMainWindow::_fsxWsSettings->getGuiShowOnlyOwnAircraft() &&
                a.getId() != FSXOWNAIRCRAFTID ) continue;
        FsxWsMainWindow::addAircraftToView(a);
    }
}

//
// Add waypoints
//
void FsxWsMainWindow::addWaypointsToView(QStandardItemModel* model, const QList<Waypoint>& waypoints)
{
    Waypoint w;
    foreach( w, waypoints ){
        FsxWsMainWindow::addWaypointToView(model, w);
    }
}
